from sklearn import svm
from sklearn import ensemble
from sklearn import neighbors
from sklearn import neural_network
from sklearn import preprocessing
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
import numpy as np


def predict(train_info, train_data, predict_data):
	if 'svm' in train_info:
		return SVM_predict(train_info, train_data, predict_data)
	if 'ensemble' in train_info:
		return Ensemble_predict(train_info, train_data, predict_data)
	if 'neighbors' in train_info:
		return Neighbor_predict(train_info, train_data, predict_data)
	if 'neural_network' in train_info:
		return Neural_predict(train_info, train_data, predict_data)


def SVM_predict(tarin_info, train_data, predict_data):
	clf = eval(tarin_info)
	x, y = train_data['data'], train_data['result']
	scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
	scaler.fit(x)
	x = scaler.transform(x)
	clf.fit(x, y)
	kfold = KFold(n_splits=10)
	scores = cross_val_score(clf, x, y, cv=kfold, n_jobs=1)
	print("scores:" + str(np.mean(scores)))
	result = clf.predict(predict_data)
	return result


def Ensemble_predict(train_info, train_data, predict_data):
	clf = eval(train_info)
	x, y = train_data['data'], train_data['result']
	scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
	scaler.fit(x)
	x = scaler.transform(x)
	clf.fit(x, y)
	kfold = KFold(n_splits=10)
	scores = cross_val_score(clf, x, y, cv=kfold, n_jobs=1)
	print("scores:" + str(np.mean(scores)))
	result = clf.predict(predict_data)
	return result


def Neighbor_predict(train_info, train_data, predict_data):
	clf = eval(train_info)
	x, y = train_data['data'], train_data['result']
	scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
	scaler.fit(x)
	x = scaler.transform(x)
	clf.fit(x, y)
	kfold = KFold(n_splits=10)
	scores = cross_val_score(clf, x, y, cv=kfold, n_jobs=1)
	print("scores:" + str(np.mean(scores)))
	result = clf.predict(predict_data)
	return result


def Neural_predict(train_info, train_data, predict_data):
	clf = eval(train_info)
	x, y = train_data['data'], train_data['result']
	scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
	scaler.fit(x)
	x = scaler.transform(x)
	kfold = KFold(n_splits=10)
	scores = cross_val_score(clf, x, y, cv=kfold, n_jobs=1)
	print("scores:" + str(np.mean(scores)))
	clf.fit(x, y)
	result = clf.predict(predict_data)
	return result
